package com.px.pa.modulars.solr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.px.basic.alone.core.base.BaseApiController;
import com.pig4cloud.pig.common.core.util.R;
import com.px.basic.alone.security.annotation.Inner;
import com.px.pa.constants.ActivityStatusEnum;
import com.px.pa.constants.IsTouristsEnum;
import com.px.pa.modulars.circle.entity.*;
import com.px.pa.modulars.circle.service.*;
import com.px.pa.modulars.circle.vo.CircleActivityFindVo;
import com.px.pa.modulars.solr.service.SolrDockingService;
import com.px.pa.modulars.solr.service.SolrService;
import lombok.RequiredArgsConstructor;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;


@RestController
@RequiredArgsConstructor
@RequestMapping("/solr")
public class SolrTimerController extends BaseApiController {



    private final SolrService solrService;

    private final CircleDynamicService circleDynamicService;

    private final SolrDockingService solrDockingService;

    private final CircleDockingService circleDockingService;

    @Value("${solr_core.dyCore}")
    private String collection ;


    @Value("${solr_core.dockingCore}")
    private String dockingCollection;



    //每晚0点更新知识索引库
    @Scheduled(cron = "0 0 0 * * ?")
    @GetMapping("/dynamicSolr")
    @Inner(value = false)
    public void dynamicSolr(){
        try {
            List<CircleDynamic> list = circleDynamicService.list(new QueryWrapper<CircleDynamic>().ne("learning_type",2));
            solrService.dynamicSolr(collection,list);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
    }

    //每晚0点更新资源索引库
    @Scheduled(cron = "0 0 0 * * ?")
    @Inner(value = false)
    @GetMapping("/dockingSolr")
    public void dockingSolr(){

        List<CircleDocking> dockingList = circleDockingService.list(new QueryWrapper<CircleDocking>()
                .eq("state",2).eq("is_audit",1).eq("is_display",0));

        dockingList.forEach(a->{
            try {
                solrDockingService.dockingCommit(dockingCollection,a);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (SolrServerException e) {
                e.printStackTrace();
            }
        });
    }

    @GetMapping("/deleteVideo")
    @Inner(value = false)
    public void  deleteVideo(String tenant){
        try {
            String query = "dynamicType:2 tenant="+tenant;
            solrService.deleteByQuery(collection,query);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }

    }



}
